﻿using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

public enum SoundTypeID : int
{
    TYPE_BG = 1,    //背景音效
    TYPE_2D = 2,    //2d音效
    TYPE_3D = 3,    //3d音效
}

public enum SoundStatus
{
    loading = 1,
    playing = 2,
    stop = 3,
}

public class SoundMgr : IZUpdate
{
    Dictionary<string, int> sceneBGDic = new Dictionary<string, int>();
    Dictionary<int, SoundStatus> soundStatus = new Dictionary<int, SoundStatus>();
    Dictionary<SoundTypeID, ISound> soundList = new Dictionary<SoundTypeID, ISound>();

    public SoundMgr()
    {
        sceneBGDic.Clear(); soundStatus.Clear(); soundList.Clear(); Init();
        ZEventCenter.AddEventListener(ConstData.SCENE_LOAD_END, OnSceneLoaded);
    }

    private bool mute = false;
    /// <summary>
    /// 是否静音 针对所有音效类型有效
    /// </summary>
    public bool Mute
    {
        set
        {
            if (value != mute)
            {
                mute = value;
                AudioListener.pause = mute;
            }
        }
    }

    private void Init()
    {
        soundList[SoundTypeID.TYPE_BG] = new BGSound();
        soundList[SoundTypeID.TYPE_2D] = new Effect2DSound();
        soundList[SoundTypeID.TYPE_3D] = new Effect3DSound();

        for (int i = 0; i < TableDataMgr.SceneClassMgr.mDataCount; i++)
        {
            var item = TableDataMgr.SceneClassMgr.GetItemByIdx(i);
            sceneBGDic.Add(item.SceneName, item.BGMusic);
        }
    }

    private void OnSceneLoaded(ZEvent ev)
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList.ElementAt(i).Value.OnSceneLoaded();
        }
        int soundId;
        sceneBGDic.TryGetValue(Application.loadedLevelName, out soundId);
        if (soundId > 0) { PlaySound(soundId); }
    }

    public void ZUpdate()
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList.ElementAt(i).Value.Update();
        }
    }
    
    /// <summary>
    /// 播放音乐   循环播放的一定要停止播放
    /// </summary>
    /// <param name="id">对应模板id</param>
    public void PlaySound(int id, OnLoadArg arg = null)
    {
        BehaviourUtil.StartCoroutine(RunPlaySound(id, arg));
    }

    private IEnumerator RunPlaySound(int id, OnLoadArg arg)
    {
        var soundTemp = TableDataMgr.SoundTempMgr.GetItemById(id);
        if (soundTemp != null)
        {
            soundStatus[id] = SoundStatus.loading;
            string path = PathUtil.Sound_Path + soundTemp.ResName + ".mp3.uab";

            OnLoadArg arg1 = new OnLoadArg();
            yield return BehaviourUtil.StartCoroutine(ZMgr.ResourceMgr.CoroutineLoad(path, arg1));
            if (soundStatus[id] == SoundStatus.stop)
            {
                //加载过程中停止了播放
                yield break;
            }

            AudioClip clip = arg1.asset as AudioClip;
            ISound sound = soundList[(SoundTypeID)soundTemp.SoundType];

            AudioSource asource = sound.Play(soundTemp, clip);
            soundStatus[id] = SoundStatus.playing;
            if (arg != null)
            {
                arg.asset = asource;
                arg.loadOk = true;
            }
        }
    }

    public void Pause()
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList.ElementAt(i).Value.Pause();
        }
    }

    public void RePlay()
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList.ElementAt(i).Value.RePlay();
        }
    }

    /// <summary>
    /// 停止播放 指定音效id 的音效
    /// </summary>
    /// <param name="id">音效在音效模板表对应id</param>
    public void StopSound(int id)
    {
        if (soundStatus.ContainsKey(id))
        {
            if (soundStatus[id] == SoundStatus.playing)
            {
                //已经在播放中 调用停止
                var table_SoundTemp = TableDataMgr.SoundTempMgr.GetItemById(id);
                if (table_SoundTemp != null)
                {
                    soundList[(SoundTypeID)table_SoundTemp.SoundType].StopById(id);
                    soundStatus[id] = SoundStatus.stop;
                }
            }
            else
            {
                //处以加载状态中  加上停止播放状态 加载完后处理停止
                soundStatus[id] = SoundStatus.stop;
            }
        }
    }

    /// <summary>
    /// 改变频率 指定音效id 的音效
    /// </summary>
    /// <param name="id">音效在音效模板表对应id</param>
    public void SetPitch(int id, float pitch)
    {
        if (soundStatus.ContainsKey(id))
        {
            if (soundStatus[id] == SoundStatus.playing)
            {
                //已经在播放中  调用停止
                var table_SoundTemp = TableDataMgr.SoundTempMgr.GetItemById(id);
                if (table_SoundTemp != null)
                {
                    soundList[(SoundTypeID)table_SoundTemp.SoundType].SetPitch(id, pitch);
                }
            }
        }
    }

    /// <summary>
    /// 停止当前播放所有音效 停止播放所有类型的
    /// </summary>
    public void StopSound()
    {
        for (int i = 0; i < soundList.Count; i++)
        {
            soundList.ElementAt(i).Value.Stop();
        }
    }

    /// <summary>
    /// 停止指定 soundType 当前播放的所有音效
    /// </summary>
    /// <param name="soundType">要停止播放的音效类型</param>
    public void StopSound(SoundTypeID soundType)
    {
        soundList[soundType].Stop();
    }

    /// <summary>
    /// 设置指定 soundType 是否静音
    /// </summary>
    /// <param name="mute">是否静音</param>
    /// <param name="soundType">音效类型</param>
    public void SetMute(bool mute, int soundType)
    {
        soundList[(SoundTypeID)soundType].Mute = mute;
    }
}